package com.hd.trans.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.hd.trans.R;

public class DashedLineView extends View {

    private int dashGap = 10;
    private int dashWidth = 10;
    private int color = Color.BLACK;
    private int orientation = LinearLayout.HORIZONTAL;

    private final Paint mPaint = new Paint();

    public DashedLineView(Context context) {
        this(context,null);
    }
    public DashedLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public DashedLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }
    public DashedLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        if (attrs != null){
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.DashedLineView);
            dashGap = attributes.getDimensionPixelSize(R.styleable.DashedLineView_android_dashGap,dashGap);
            dashWidth = attributes.getDimensionPixelSize(R.styleable.DashedLineView_android_dashWidth,dashWidth);
            color = attributes.getColor(R.styleable.DashedLineView_android_color,color);
            orientation = attributes.getInt(R.styleable.DashedLineView_android_orientation,orientation);
            attributes.recycle();
        }

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(color);
        mPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));

    }

    public void setDashGap(int dashGap) {
        this.dashGap = dashGap;
        mPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, this.dashGap}, 0));
        invalidate();
    }

    public void setDashWidth(int dashWidth) {
        this.dashWidth = dashWidth;
        mPaint.setPathEffect(new DashPathEffect(new float[]{this.dashWidth, dashGap}, 0));
        invalidate();
    }

    public void setColor(int color) {
        this.color = color;
        mPaint.setColor(this.color);
        invalidate();
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int viewWidth = getWidth();
        int viewHeight = getHeight();


        if (orientation == LinearLayout.HORIZONTAL){
            mPaint.setStrokeWidth(viewHeight*2);
            canvas.drawLine(0, 0, viewWidth, 0, mPaint);
        }
        else {
            mPaint.setStrokeWidth(viewWidth*2);
            canvas.drawLine(0, 0, 0, viewHeight, mPaint);
        }

    }


}
